package com.aizhixin.lab.course.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.PageData;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.course.domain.*;
import com.aizhixin.lab.course.course.service.ReleaseSignInService;
import com.aizhixin.lab.course.course.service.StudentCourseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/course/student")
@Api(value = "学生课程相关API")
public class StudentCourseController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private StudentCourseService courseService;

    @Autowired
    private ReleaseSignInService releaseSignInService;

    @RequestMapping(value = "/getList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程列表", response = Void.class, notes = "查询课程列表<br>@author hsh")
    public ResponseEntity<?> getList(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "name", required = false) @RequestParam(value = "name", required = false) String name,
                                     @ApiParam(value = "pageNumber 起始页") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
                                     @ApiParam(value = "pageSize 每页的限制数目") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        PageData<CourseStuListDomain> result = courseService.getList(account.getId(), name, pageNumber, pageSize);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/sign", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "学生签到", response = Void.class, notes = "学生签到<br>@author lwq")
    public ResponseEntity<?> sign(@RequestHeader("Authorization") String token,
                                       @ApiParam(value = "code", required = true) @RequestParam(value = "code", required = true) String code) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> map = releaseSignInService.signIn(code, account.getId());

        return new ResponseEntity(map, HttpStatus.OK);
    }
    @RequestMapping(value = "/countStuUnSignNum", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "获取学生未签到数", response = Void.class, notes = "获取学生未签到数<br>@author lwq")
    public ResponseEntity<?> countStuUnSignNum(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Integer stuUnSignNum = releaseSignInService.countStuUnSign(account.getId());

        return new ResponseEntity(stuUnSignNum, HttpStatus.OK);
    }

    @RequestMapping(value = "/getCourse", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程,预览时用", response = Void.class, notes = "查询课程,预览时用<br>@author hsh")
    public ResponseEntity<?> getCourse(@RequestHeader("Authorization") String token,
                                       @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        CourseStuDomain result = courseService.getCourse(courseId, account.getId());
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/stuLookFile", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "学生查看文件", response = Void.class, notes = "学生查看文件<br>@author lwq")
    public ResponseEntity<?> stuLookFile(@RequestHeader("Authorization") String token,
                                           @ApiParam(value = "fileId", required = true) @RequestParam(value = "fileId", required = true) Long fileId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        courseService.look(fileId, account);
        return new ResponseEntity(true, HttpStatus.OK);
    }
    @RequestMapping(value = "/findStuSignRecord", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "获取学生当日签到信息", notes = "获取学生当日签到信息<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuSignRecordDomain.class)})
    public ResponseEntity<?> findStuSignRecord(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String,Object> result=new HashMap<>();
        List<StuSignRecordDomain> stuSignRecord = releaseSignInService.findStuSignRecord(account.getId());
        result.put(ApiReturnConstants.SUCCESS,true);
        result.put(ApiReturnConstants.DATA,stuSignRecord);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findStuProjectSignRecord", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "获取学生当日项目签到信息", notes = "获取学生当日项目签到信息<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuSignRecordDomain.class)})
    public ResponseEntity<?> findStuProjectSignRecord(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String,Object> result=new HashMap<>();
        List<StuSignRecordDomain> stuSignRecord = releaseSignInService.findSignRecord(account.getId());
        result.put(ApiReturnConstants.SUCCESS,true);
        result.put(ApiReturnConstants.DATA,stuSignRecord);
        return new ResponseEntity(result, HttpStatus.OK);
    }
}
